package adsf.adminisadsf.gugedianzishichang.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import java.util.ArrayList;

import adsf.adminisadsf.gugedianzishichang.holder.BaseHolder;
import adsf.adminisadsf.gugedianzishichang.holder.MoreHodel;
import adsf.adminisadsf.gugedianzishichang.utils.UIUtils;


/**
 * Created by administrator on 17/5/13.
 */

public abstract class MyBaseAdapter<T> extends BaseAdapter {


    //给加载更多设置标记
    private static final int TYPE_NORMAL = 0; //正常布局
    private static final int TYPE_MORE = 1;   //加载更多布局



    public ArrayList<T> data;
    //来个构造方法
    public MyBaseAdapter(ArrayList<T> data)
    {
        this.data = data;
    }


    @Override
    public int getCount() {
        return data.size() + 1;//表示多加载一个布局
    }

    @Override
    public T getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    //返回加载更多布局
    @Override
    public int getViewTypeCount() {
        return 2;           //表示加载两种布局样式
    }

    //返回具体位置类型
    @Override
    public int getItemViewType(int position) {

        if (position == getCount() - 1)
        {
            return TYPE_MORE;
        }else
        {
            return getInnerType();
        }

    }

    //让类可以修改,可以加载更多布局  默认普通样式
    public int getInnerType()
    {
        return TYPE_NORMAL;  //默认普通样式
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        BaseHolder baseHolder = null;
        if (convertView == null)
        {
            if (getItemViewType(position) == TYPE_MORE)
            {
                //加载更多
                baseHolder = new MoreHodel(hasMore());
            }else
            {
                //返回当前的页面,但是不知道当前页面是什么样子的所有来一个抽象方法让继承者实现
                baseHolder = getHodeler();
            }

        }else
        {
            baseHolder = (BaseHolder) convertView.getTag();
        }


        //根据数据刷新界面
        if (getItemViewType(position) != TYPE_MORE)
        {
            baseHolder.setData(getItem(position));
        }else
        {
            //加载更多
            MoreHodel moreHodel = (MoreHodel) baseHolder;
            //判断只有在有更多数据下才加载更多数据
            if (moreHodel.getData() == MoreHodel.STATE_MORE_MORE)
            {
                //开始加载更多布局
                loadMore(moreHodel);
            }
        }

        return baseHolder.getmRootView();
    }


    //子类可以重写,表示是否加载更多布局
    public boolean hasMore()
    {
        return true;
    }

    //标记是否在加载更多
    public boolean isLoadMor = false;

    //加载更多
    public void loadMore(final MoreHodel hodel)
    {
        if (!isLoadMor)
        {
            isLoadMor = true;
            new Thread()
            {
                @Override
                public void run() {
                    super.run();

                    final ArrayList<T> moreData = onLoadMore();
                    //有了更多数据之后就要刷新界面,主线程

                    UIUtils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {

                            //判断数据是否为空
                            if (moreData != null)
                            {
                                //进行判断是否有更多数据,根据没野加载多少条数据
                                if (moreData.size() < 20)
                                {
                                    //判断加载那个布局需要MoreHoadel 那只能穿个参数了
                                    hodel.setData(MoreHodel.STATE_MORE_NONE);
                                    Toast.makeText(UIUtils.getContext(),
                                            "没有更多数据了", Toast.LENGTH_SHORT)
                                            .show();
                                }else
                                {
                                    hodel.setData(MoreHodel.STATE_MORE_MORE);
                                }


                                //有了数据那么久添加到集合中
                                data.addAll(moreData);
                                //刷新界面
                                MyBaseAdapter.this.notifyDataSetChanged();


                            }else
                            {
                                //加载更多失败
                                hodel.setData(MoreHodel.STATE_MORE_ERROR);
                            }

                            isLoadMor = false;
                        }
                    });


                }
            }.start();

        }

    }



    //返回当前页面
    public abstract BaseHolder<T> getHodeler();

    //加载更多数据
    public abstract ArrayList<T> onLoadMore();
}















